package com.cq.oa.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cq.oa.domain.sm.User;
import com.cq.oa.service.sm.UserService;

/**
 * 登录
 * @author lilang
 *
 */
public class LoginServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6743343870131196414L;

	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		super.doGet(req, resp);
	}

	/**
	 * 初始化
	 */
	private void initSpringBean(){
		if(userService==null){
			WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
			userService = (UserService) wc.getBean("userService");
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		HttpSession session = req.getSession();
		initSpringBean();
		String userName = req.getParameter("userName");
		String userPwd = req.getParameter("userPwd");
		String ip = req.getHeader("x-forwarded-for");
		System.out.println(userName);
		System.out.println(userPwd);
		System.out.println(ip);
		System.out.println(userService);
		System.out.println(session.getId());
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("username", userName);
		User user = userService.findUser(params);
		if(user!=null)
		System.out.println(user.getCname());
	}

}
